package servlet;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import KMLGenerator.KMLCreator;
import KMLGenerator.MEStyle;
import KMLGenerator.MapEdge;

import universityMap.Agreement;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Timer;

import logger.GRIALogger;

import dBConnection.GRIAdBUpdater;
import dataRetriever.DataCreator;

/**
 * This class manage the request for KML documents coming from the index page of
 * UNICAM GRIA. At every request, it generates a random named KML and try to
 * delete the previous KML document (if any).
 * 
 * @author Andrea Piermarteri, Matteo Micheletti, Diego Cervellini.
 * 
 */
public class ServletLink extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		GRIALogger.log("http request (POST);");

		GRIAdBUpdater.getInstance("jdbc:mysql://193.205.92.106:3306/",
				"fakeESSE3", "root", "progetto", "com.mysql.jdbc.Driver");

		ArrayList<Agreement> input = null;
		try {
			input = DataCreator.createDataFromDBConnector(
					"jdbc:mysql://193.205.92.106:3306/", "GRIAdb", "root",
					"progetto");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// code for deleting the old kml file
		try {
			if (!request.getParameter("oldrandom").trim().equalsIgnoreCase(
					"std")) {
				new File(
						"C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/GRIA/kml/"
								+ request.getParameter("oldrandom") + ".kml")
						.delete();
				GRIALogger.log("deleted " + request.getParameter("oldrandom")
						+ ".kml;");
			}
		} catch (NullPointerException e) {
			// TODO: handle exception
		}

		ArrayList<Agreement> data = DataCreator.select(input, request
				.getParameter("from"), request.getParameter("to"), request
				.getParameter("agreement"), request.getParameter("country"));

		if (data.size() > 0) {
			KMLCreator k = new KMLCreator(data,
					"C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/GRIA/kml/"
							+ request.getParameter("random") + ".kml");
			
			if (request.getParameter("from").trim().equalsIgnoreCase("")
					&& request.getParameter("to").trim().equalsIgnoreCase("")
					&& request.getParameter("agreement").trim().equalsIgnoreCase("")
					&& request.getParameter("country").trim().equalsIgnoreCase("")){
				for(MapEdge e : k.mapEdges){
					MEStyle style = e.getStyle();
					style.setColor("64FFFFFF");
					e.setStyle(style);
				}
			}
			
			k.getKML();
			GRIALogger.log("created " + request.getParameter("random")
					+ ".kml;");

			// after 60 seconds, the kml is destroyed in any case
			Timer timer = new Timer();
			TimedGarbageTask t = new TimedGarbageTask(request
					.getParameter("random"));
			timer.schedule(t, 1000 * 20);
			response
					.sendRedirect("http://cssdcc.cs.unicam.it/home/index.php?option=com_content&view=article&id=46&Itemid=53&OldKmlName="
							+ request.getParameter("oldrandom")
							+ "&kmlName="
							+ request.getParameter("random"));
		} else if ((data = DataCreator.select(input,
				request.getParameter("to"), request.getParameter("from"),
				request.getParameter("agreement"), request
						.getParameter("country"))).size() > 0) {
			KMLCreator k = new KMLCreator(data,
					"C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/GRIA/kml/"
							+ request.getParameter("random") + ".kml");

			if (request.getParameter("from").trim().equalsIgnoreCase("")
					&& request.getParameter("to").trim().equalsIgnoreCase("")
					&& request.getParameter("agreement").trim().equalsIgnoreCase("")
					&& request.getParameter("country").trim().equalsIgnoreCase("")){
				for(MapEdge e : k.mapEdges){
					MEStyle style = e.getStyle();
					style.setColor("64FFFFFF");
					e.setStyle(style);
				}
			}

				k.getKML();

			// after 60 seconds, the kml is destroyed in any case
			Timer timer = new Timer();
			TimedGarbageTask t = new TimedGarbageTask(request
					.getParameter("random"));
			timer.schedule(t, 1000 * 20);
			response
					.sendRedirect("http://cssdcc.cs.unicam.it/home/index.php?option=com_content&view=article&id=46&Itemid=53&OldKmlName="
							+ request.getParameter("oldrandom")
							+ "&kmlName="
							+ request.getParameter("random"));

		} else {
			// after 60 seconds, the kml is destroyed in any case
			Timer timer = new Timer();
			TimedGarbageTask t = new TimedGarbageTask(request
					.getParameter("random"));
			timer.schedule(t, 1000 * 20);
			response
					.sendRedirect("http://cssdcc.cs.unicam.it/home/index.php?option=com_content&view=article&id=46&Itemid=53&OldKmlName="
							+ request.getParameter("oldrandom")
							+ "&kmlName=std");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}